package com.huanshi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanshi.entity.PmsProduct;
import com.huanshi.mapper.PmsProductMapper;
import com.huanshi.module.pms.vo.PmsProductDetailVO;
import com.huanshi.service.PmsProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * spu描述的是一个抽象性的商品，比如 iphone8Service业务层处理
 * 
 * @author huanshi
 * @date 2023-07-02
 */
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsProductService {
    @Resource
    private PmsProductMapper productMapper;

    @Override
    public IPage<PmsProductDetailVO> findPage(Page<PmsProduct> productPage, Integer categoryId, String productName) {

        LambdaQueryWrapper<PmsProduct> queryWrap = new LambdaQueryWrapper<>();
        if (categoryId != null && Objects.equals(productName, "")) {
            queryWrap.eq(PmsProduct::getCid2, categoryId);
        }
        if (!Objects.equals(productName, "")) {
            queryWrap.like(PmsProduct::getName, productName);
        }
        return productMapper.selectPage(productPage, queryWrap).convert(result -> {
            PmsProductDetailVO vo = new PmsProductDetailVO();
            BeanUtils.copyProperties(result, vo);
            return vo;
        });
    }

    @Override
    public IPage<PmsProductDetailVO> findRecommendPage(Page<PmsProduct> productPage) {
        LambdaQueryWrapper<PmsProduct> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(PmsProduct::getRecommend, 1);
        return productMapper.selectPage(productPage, queryWrap).convert(result -> {
            PmsProductDetailVO vo = new PmsProductDetailVO();
            BeanUtils.copyProperties(result, vo);
            return vo;
        });
    }
}
